<template>

    <!-- 注册页面 -->
    <div style="width: 500px; margin: 0 auto;">
      <el-form :model="registerForm" :rules="rules" ref="registerForm" label-width="100px" class="register-form">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="registerForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirm_password">
          <el-input type="password" v-model="registerForm.confirm_password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email"></el-input>
        </el-form-item>
        <!-- 展示手机号 -->
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="registerForm.phone"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('registerForm')">注册</el-button>
          <el-button @click="resetForm('registerForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>

  <script>
  import {register} from '@/api/user/user'
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.registerForm.confirm_password !== '') {
            this.$refs.registerForm.validateField('confirm_password');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.registerForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        registerForm: {
          username: '',
          email: '',
          phone: '',
          password: '',
          confirm_password: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
          ],
          phone: [
            { required: true, message: '请输入电话号码', trigger: 'blur' },
            { min: 11, max:11,message: '密码长度为11位', trigger: 'blur' },
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          confirm_password: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            register(this.registerForm).then(ret => {
              this.$message({
                message: '注册成功',
                success: true,
                type: 'success'
              });
              // 注册成功之后重置表单
              this.resetForm('registerForm');
              this.$router.push({ path: '/login' });
            }).catch(error => {
              this.$message.error('注册失败，请检查输入信息');
            });
          } else {
            console.log('错误提交!!');
            return false;
          }
        });
      },

      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
  </script>

  <style scoped>
  .register-form {
    margin-top: 20px;
  }
  </style>
